package com.golddaymicromall.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.activity.recharge.RemittanceRegAddActivity;
import com.golddaymicromall.adapter.RemittanceRegisterAdapter;
import com.golddaymicromall.bean.RemittanceRegisterInfo;
import com.golddaymicromall.module.ListViewLoadMore;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by 王立强 on 2016/3/3.
 */
public class HuiKuanDengJiFragment extends Fragment implements View.OnClickListener, ListViewLoadMore.IsLoadingListener {
    private View view;
    private ListViewLoadMore mRecharge_Online;
    private RemittanceRegisterAdapter rechargeOnLineAdapter;
    private List<RemittanceRegisterInfo> listRemitRegInfo;
    private List<RemittanceRegisterInfo> listAll = new ArrayList<RemittanceRegisterInfo>();
    private TextView mRemittanceRegAdd;
    private Handler handler = new Handler();
    int CurrentPageNum = 0;
    String Logid;
    ProgressBar progress;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_huikuandengji, container, false);
        initView();
        Logid = SPUtils.get(getContext(), "Id", "").toString();
        return view;
    }

    private void initView() {
        progress = (ProgressBar) view.findViewById(R.id.progress);
        mRecharge_Online = (ListViewLoadMore) view.findViewById(R.id.Recharge_zaixianhuikuan);
        mRemittanceRegAdd = (TextView) view.findViewById(R.id.zaixianhuikuan_add);
        mRemittanceRegAdd.setOnClickListener(this);
        mRecharge_Online.setOnLoadingListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zaixianhuikuan_add:
                Intent intent_remitReg_add = new Intent(getActivity(), RemittanceRegAddActivity.class);
                startActivity(intent_remitReg_add);
                break;
        }
    }

    public void attemptGetRechargeOnlineTask(int CurrentPageNum) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.remittanceregister.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", Logid);
        form.put("current_pagenum", String.valueOf(CurrentPageNum));
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> progress.setVisibility(View.VISIBLE))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    progress.setVisibility(View.INVISIBLE);

                    listRemitRegInfo = new ArrayList<RemittanceRegisterInfo>();
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getActivity(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                        } else {
                            listRemitRegInfo = JSON.parseArray(result, RemittanceRegisterInfo.class);
                            if (CurrentPageNum > 1) {
                                listAll.addAll(listRemitRegInfo);
                            } else if (CurrentPageNum == 1) {
                                listAll.clear();
                                listAll.addAll(listRemitRegInfo);
                            }
                            rechargeOnLineAdapter = new RemittanceRegisterAdapter(listAll, getActivity());
                            mRecharge_Online.setAdapter(rechargeOnLineAdapter);
                            rechargeOnLineAdapter.notifyDataSetChanged();
                            if (CurrentPageNum > 1) {
                                mRecharge_Online.setSelection(mRecharge_Online.getCount() - 1);
                            }
                        }
                    } else {
                        Toast.makeText(getActivity(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }


                },throwable -> {
                    progress.setVisibility(View.INVISIBLE);
                    throwable.printStackTrace();
                });

    }

    @Override
    public void onLoad() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                CurrentPageNum++;
                attemptGetRechargeOnlineTask(CurrentPageNum);
                rechargeOnLineAdapter.notifyDataSetChanged();
                mRecharge_Online.complateLoad();
            }
        }, 3000);
    }

    @Override
    public void onResume() {
        super.onResume();
        CurrentPageNum = 1;
        attemptGetRechargeOnlineTask(CurrentPageNum);
    }

}
